/******************************************************************************

  Copyright (C), 2001-2011, DCN Co., Ltd.

 ******************************************************************************
  File Name     : Board.c
  Version       : Initial Draft
  Author        : Hust
  Created       : 2023/1/14
  Last Modified :
  Description   : board
  Function List :
              GetEvent
  History       :
  1.Date        : 2023/1/14
    Author      : Hust
    Modification: Created file

******************************************************************************/



/*----------------------------------------------*
 * include headfile                             *
 *----------------------------------------------*/
//#include "stm32f1xx_hal.h"
#include "Board.h"

/*----------------------------------------------*
 * variables typedef                             *
 *----------------------------------------------*/
typedef void (*pFunction)(void);
/*----------------------------------------------*
 * macros                                       *
 *----------------------------------------------*/

/*----------------------------------------------*
 * constants                                    *
 *----------------------------------------------*/

/*----------------------------------------------*
 * external variables                           *
 *----------------------------------------------*/

/*----------------------------------------------*
 * project-wide global variables                *
 *----------------------------------------------*/
CAN_RX_MSG_STRU gRxMsg;
CAN_TX_MSG_STRU gTxMsg;

CAN_OTA_INFO_STRU gOta_info;
//#include <stddef.h>
//#include <LowLevelIOInterface.h>
__no_init  uint32_t mOtaFlag @ APP_UPDATE_KEY_ADDRESS;

MODL_INFO_STRU gModlInfo ;
TIMER_MS_STRU gTimer0;
TIMER_MS_STRU gTimer1;


FlagStatus		gRxFlag = RESET;
FlagStatus		gTxFlag = RESET;

/* TIMER_VAR_DEF(Timer0); */
FlagStatus gTimer0_start_flag = RESET;
VS_UINT	   gTimer0_cnt = 0;
SEM_EVENT_TYPE	   gTimer0_call_event = Null_Ev;

/* TIMER_VAR_DEF(Timer1); */
FlagStatus gTimer1_start_flag = RESET;
VS_UINT	   gTimer1_cnt = 0;
SEM_EVENT_TYPE	   gTimer1_call_event = Null_Ev;


/*----------------------------------------------*
 * module-wide global variables                 *
 *----------------------------------------------*/

/*----------------------------------------------*
 * external routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * internal routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * routines' implementations                    *
 *----------------------------------------------*/



#if 1
void JumpToApplication(uint32_t Addr)
{
	pFunction		JumpToApp;
	__IO uint32_t	JumpAddress;

	__set_PRIMASK(1);								//关闭全局中断，注意，必须在APP中打开全局中断，否则可能会导致APP中断程序不正�?

	if (((* (__IO uint32_t *) Addr) & 0x2FFE0000) == 0x20000000)
	{
		JumpAddress 		= * (__IO uint32_t *) (Addr + 4);
		JumpToApp			= (pFunction)	JumpAddress;
		__set_MSP(* (__IO uint32_t *) Addr);
		JumpToApp();
	}
}


#endif

void GetEvent(void)
{
    mOtaFlag=1;
}

static void Error_Handler(void)
{
  while (1)
  {
  }
}

void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
{
	HAL_CAN_GetRxMessage(hcan, CAN_FilterFIFO0,  &gRxMsg.header, gRxMsg.data);
    
}
 /**
   * @brief  Rx Fifo 0 message pending callback in non blocking mode
   * @param  CanHandle: pointer to a CAN_HandleTypeDef structure that contains
   *		 the configuration information for the specified CAN.
   * @retval None
   */
 void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
 {
   /* Get RX message */
   if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &gRxMsg.header, gRxMsg.data) != HAL_OK)
   {
	 /* Reception Error */
	 Error_Handler();
   }
 	SEQ_AddEvent((SEM_EVENT_TYPE) RxTx_Ev);
   /* Display LEDx */
   if ((gRxMsg.header.StdId == 0x321) && (gRxMsg.header.IDE == CAN_ID_STD) && (gRxMsg.header.DLC == 2))
   {
//	 LED_Display(RxData[0]);
   }
 }


 void TimerCnt(void)
{
	TIMER_CNT_SUB_BLOCK(gTimer0);
	
	TIMER_CNT_SUB_BLOCK(gTimer1);
}

 


/**
  * @brief  Configures the CAN.
  * @param  None
  * @retval None
  */
 void CAN_Config(CAN_HandleTypeDef * hcan)
{
  CAN_FilterTypeDef  sFilterConfig;

  /* Configure the CAN peripheral */
  hcan->Instance = CAN1;

//  hcan->Init.TimeTriggeredMode = DISABLE;
//  hcan->Init.AutoBusOff = DISABLE;
//  hcan->Init.AutoWakeUp = DISABLE;
//  hcan->Init.AutoRetransmission = ENABLE;
//  hcan->Init.ReceiveFifoLocked = DISABLE;
//  hcan->Init.TransmitFifoPriority = DISABLE;
//  hcan->Init.Mode = CAN_MODE_NORMAL;
//  hcan->Init.SyncJumpWidth = CAN_SJW_1TQ;
//  hcan->Init.TimeSeg1 = CAN_BS1_6TQ;
//  hcan->Init.TimeSeg2 = CAN_BS2_2TQ;
//  hcan->Init.Prescaler = 4;

  if (HAL_CAN_Init(hcan) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Configure the CAN Filter */
  sFilterConfig.FilterBank = 0;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = 0x0000;
  sFilterConfig.FilterIdLow = 0x0000;
  sFilterConfig.FilterMaskIdHigh = 0x0000;
  sFilterConfig.FilterMaskIdLow = 0x0000;
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;

  if (HAL_CAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK)
  {
    /* Filter configuration Error */
    Error_Handler();
  }

  /* Start the CAN peripheral */
  if (HAL_CAN_Start(hcan) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }

  /* Activate CAN RX notification */
  if (HAL_CAN_ActivateNotification(hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  {
    /* Notification Error */
    Error_Handler();
  }
  
  /* Configure Transmission process */
//  TxHeader.StdId = 0x321;
//  TxHeader.ExtId = 0x01;
//  TxHeader.RTR = CAN_RTR_DATA;
//  TxHeader.IDE = CAN_ID_STD;
//  TxHeader.DLC = 2;
//  TxHeader.TransmitGlobalTime = DISABLE;
}

